package com.rcl.recyclerviewdemo.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/11/3.
 * PS: Not easy to write code, please indicate.
 */
public class NameTest {
    public static void main(String[] args) {
        List<NameBean> list = new ArrayList<>();
        NameBean nameBean1 = new NameBean("张三", "22");
        NameBean nameBean2 = new NameBean("李四", "23");
        NameBean nameBean3 = new NameBean("王二", "24");
        NameBean nameBean4 = new NameBean("张三", "22");
        NameBean nameBean5 = new NameBean("张三", "25");
        NameBean nameBean6 = new NameBean("小五", "25");
        NameBean nameBean7 = new NameBean("张三", "26");
        list.add(nameBean1);
        list.add(nameBean2);
        list.add(nameBean3);
        list.add(nameBean4);
        list.add(nameBean5);
        list.add(nameBean6);
        list.add(nameBean7);

        List<NameBean> integers = returnList(list);
        for (NameBean integer : integers) {
            System.out.println(integer.getName()+","+integer.getAge());
        }
    }

    public static List<NameBean> returnList(List<NameBean> list) {
        /*for (NameBean nameBean : list) {
            nameBean.setNameAge(nameBean.getName()+nameBean.getAge());
        }*/

        Map<String, List<NameBean>> map = new HashMap<>();
        for (NameBean num : list) {
            if (map.containsKey(num.getName())&&map.containsKey(num.getAge())) {
                map.get(num.getName()).add(num);
            } else {
                List<NameBean> sNewList = new ArrayList<>();
                sNewList.add(num);
                map.put(num.getName(), sNewList);
                map.put(num.getAge(), sNewList);
            }
        }
        //System.out.println(map);
        List<NameBean> sAa = new ArrayList<>();
        Set<Map.Entry<String, List<NameBean>>> entries = map.entrySet();
        for (Map.Entry<String, List<NameBean>> entry : entries) {
            sAa.addAll(entry.getValue());
        }
        return sAa;
    }
}
